class Solution {
    public:
        vector<int> sortedSquares(vector<int>& nums) {
            vector<int> res(nums.size());
            int l=0;
            int r=nums.size()-1;
            int index=nums.size()-1;
            while(l<=r){
                if(abs(nums[l])>abs(nums[r])){
                    res[index--]=nums[l]*nums[l];
                    l++;
                }
                else{
                    res[index--]=nums[r]*nums[r];
                    r--;
                }
            }
            return res;
        }
    };